package org.abc.utils
{

	/**
	 * 正则表达式扩展
	 * @author aishi
	 *
	 */
	public class RegUtil
	{
		/**
		 * 字符正则转化函数，将任何字符串转化为正则匹配目标字符串
		 * @param value 需要转换的字符串
		 * @return String
		 *
		 */
		public static function transform(value:String):String
		{
			var regs:Array=["\\\/", "\\\^", "\\\$", "\\\.", "\\\*", "\\\+", "\\\?", "\\\(", "\\\)", "\\\[", "\\\]", "\\\{", "\\\}", "\\\|"];
			for (var j:Number=0; j < regs.length; j++)
			{
				value=value.replace(new RegExp(regs[j], "gis"), regs[j]);
			}
			return value;
		}

		/** 判断是否为中文字符串 */
		public static function matchChinese(word:String):Boolean
		{
			var pattern:RegExp=/[\u4e00-\u9fa5]/;
			return pattern.test(word)
		}

		/** 匹配邮件地址 */
		public static function matchEmail(email:String):Boolean
		{
			var pattern:RegExp=/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
			return pattern.test(email)
		}

		/** 匹配网址 */
		public static function matchURL(url:String):Boolean
		{
			var pattern:RegExp=/[a-zA-z]+:\/\/[^\s]*/;
			return pattern.test(url)
		}

		/**匹配电话号码 */
		public static function matchPhonecode(num:String):Boolean
		{
			var pattern:RegExp=/\d{3}-\d{8}|\d{4}-\d{7}/;
			return pattern.test(num)
		}

		/** 匹配QQ号 */
		public static function matchQQ(qq:String):Boolean
		{
			var pattern:RegExp=/[1-9][0-9]{4,}/;
			return pattern.test(qq)
		}

		/** 匹配邮政编码 */
		public static function matchPostalcode(num:String):Boolean
		{
			var pattern:RegExp=/[1-9]\d{5}(?!\d)/;
			return pattern.test(num)
		}

		/** 匹配身份证 */
		public static function matchIDcard(num:String):Boolean
		{
			var pattern:RegExp=/\d{15}|\d{18}/;
			return pattern.test(num)
		}

		/** 匹配时间 */
		public static function matchTime(num:String):Boolean
		{
			var pattern:RegExp=/([0-1]?[0-9]|2[0-3]):([0-5][0-9])/;
			return pattern.test(num)
		}

		/** 匹配日期 */
		public static function matchData(num:String):Boolean
		{
			var pattern:RegExp=/(1[0-2]|0?[1-9])[-.\/](0?[1-9]|[12][0-9]|3[01])[-.\/](dddd))/;
			return pattern.test(num)
		}

		/** 截取字符串 */
		public static function interception(source:String, prefix:String, suffix:String):String
		{
			var reg:RegExp=new RegExp("(?<=" + prefix + ").*?(?=" + suffix + ")", "s");
			var obj:String=reg.exec(source).toString();
			return obj
		}

		/** 匹配多项 */
		public static function matchMore(source:String, prefix:String, suffix:String):Array
		{
			var reg:RegExp=new RegExp("(?<=" + prefix + ").*?(?=" + suffix + ")", "g");
			var obj:Array=source.match(reg)
			return obj
		}

	}
}
